package com.shenzhenai.composebasic.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

/**
 * @author huangzehui
 * @date 2025/9/15 13:48 周一
 * @description
 *
 * 导航使用
 * https://www.bilibili.com/video/BV1nu411B7jA?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.videopod.sections
 */
@Composable
fun NavSample() {
    // 导航控制器
    val navController = rememberNavController()

    NavHost(
        navController = navController,
        // 开始的页面
        startDestination = "First",
        modifier = Modifier,
    ) {
        composable(
            // 页面的路径
            route = "First",
            // 参数
            arguments = listOf(),
            // 深层链接
            deepLinks = listOf(),
            // 页面
            content = {
                FirstScreen(navController = navController)
            },
        )

        composable(
            // 页面的路径
            route = "Second",
            // 参数
            arguments = listOf(),
            // 深层链接
            deepLinks = listOf(),
            // 页面
            content = {
                SecondScreen(navController = navController)
            },
        )

        composable(
            // 页面的路径
            route = "Third",
            // 参数
            arguments = listOf(),
            // 深层链接
            deepLinks = listOf(),
            // 页面
            content = {
                ThirdScreen(navController = navController)
            },
        )
    }
}

@Composable
fun FirstScreen(navController: NavHostController) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Red),
        // 横向居中
        horizontalAlignment = Alignment.CenterHorizontally,
        // 垂直居中
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = "First Screen")
        Button(onClick = {
            navController.navigate("Second")
        }) {
            Text(text = "Navigate to second screen")
        }
    }
}

@Composable
fun SecondScreen(navController: NavHostController) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Green),
        // 横向居中
        horizontalAlignment = Alignment.CenterHorizontally,
        // 垂直居中
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = "Second Screen")
        Button(onClick = {
            navController.navigate("Third")
        }) {
            Text(text = "Navigate to third screen")
        }
    }
}

@Composable
fun ThirdScreen(navController: NavHostController) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Blue),
        // 横向居中
        horizontalAlignment = Alignment.CenterHorizontally,
        // 垂直居中
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = "Third Screen")
        Button(onClick = {
//            navController.navigate("First")
            navController.popBackStack(
                "First",
                // 退出的时候，是否需要退出指定的页面，
                // true: 退出到 First 页面后，First 也会被退出
                // false: 退出到 First 页面后，First 页面不退出，停留在 First 页面
                inclusive = false
            )
        }) {
            Text(text = "Navigate back to root screen")
        }
    }
}

@Preview
@Composable
fun NavSamplePreview() {
    NavSample()
}